﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using VDataAdapter.EventManagement;

namespace VDataAdapter.QueueManagement
{
    /// <summary>
    /// This class implement both interface to serve both the PriceServerEventHandler and Worker
    /// </summary>
    class SimpleEventQueue : IEventProviderQueue, IEventStoteredQueue
    {
        private FirstTimeEvent _firstTimeEvent;
        private Queue<MarketInfosEvent> _marketInfosEventQueue;
        private Queue<PtTransactionsChangedEvent> _ptTransactionsChangedEvent;
        private Queue<Top3PricesEvent> _top3PricesEventQueue;
        private Queue<PtTopPricesChangedEvent> _ptTopPricesChangedEventQueue;
        private Queue<TransactionsEvent> _transactionsEventQueue;

        private Object _firstTimeEventLock = new Object(); 
        private Object _ptTransactionsChangedEventLock = new Object();


        //Simple constructor
        public SimpleEventQueue()
        {
            _top3PricesEventQueue = new Queue<Top3PricesEvent>();
            _ptTopPricesChangedEventQueue = new Queue<PtTopPricesChangedEvent>();
            _ptTransactionsChangedEvent = new Queue<PtTransactionsChangedEvent>();
            _transactionsEventQueue = new Queue<TransactionsEvent>();
            _marketInfosEventQueue = new Queue<MarketInfosEvent>();
        }

        //Get and set first time event
        public FirstTimeEvent GetFirstTimeEvent()
        {
            lock (_firstTimeEventLock)
            {
                var e = _firstTimeEvent;
                _firstTimeEvent = null; // remove first time event
                return e;
            }
        }
        public void PutFirstTimeEvent(FirstTimeEvent eventItem)
        {
            lock (_firstTimeEventLock)
            {
                _firstTimeEvent = eventItem;
            }
        }


        //Get and set market info event
        public MarketInfosEvent GetMarketInfosEvent()
        {
            lock (_marketInfosEventQueue)
            {
                if (_marketInfosEventQueue.Count > 0)
                {
                    return _marketInfosEventQueue.Dequeue();
                }
                else
                {
                    return null;
                }
            }
        }
        public void PutMarketInfosEvent(MarketInfosEvent eventItem)
        {
            lock (_marketInfosEventQueue)
            {
                _marketInfosEventQueue.Enqueue(eventItem);
            }
        }


        //Get and set pt transaction event
        public PtTransactionsChangedEvent GetPtTransactionsEvent()
        {
            lock (_ptTransactionsChangedEventLock)
            {
                if (_ptTransactionsChangedEvent.Count > 0)
                {
                    return _ptTransactionsChangedEvent.Dequeue();
                }
                else
                {
                    return null;
                }
            }
        }
        public void PutPtTransactionsEvent(PtTransactionsChangedEvent eventItem)
        {
            lock (_ptTransactionsChangedEventLock)
            {
                _ptTransactionsChangedEvent.Enqueue(eventItem);
            }
        }

        
        //Get and put Top price event
        public Top3PricesEvent GetTop3PricesEvent()
        {
            lock (_top3PricesEventQueue)
            {
                if (_top3PricesEventQueue.Count > 0)
                {
                    return _top3PricesEventQueue.Dequeue();
                }
                else
                {
                    return null;
                }
            }
        }
        public void PutTop3PricesEvent(Top3PricesEvent eventItem)
        {
            lock (_top3PricesEventQueue)
            {
                _top3PricesEventQueue.Enqueue(eventItem);
            }
        }


        //Get and put Pt top price event
        public PtTopPricesChangedEvent GetPtTopPricesEvent()
        {
            lock (_ptTopPricesChangedEventQueue)
            {
                if (_ptTopPricesChangedEventQueue.Count > 0)
                {
                    return _ptTopPricesChangedEventQueue.Dequeue();
                }
                else
                {
                    return null;
                }
            }
        }
        public void PutPtTopPricesEvent(PtTopPricesChangedEvent eventItem)
        {
            lock (_ptTopPricesChangedEventQueue)
            {
                _ptTopPricesChangedEventQueue.Enqueue(eventItem);
            }
        }


        public void PutTransactionsEvent(TransactionsEvent eventItem)
        {
            lock (_transactionsEventQueue)
            {
                _transactionsEventQueue.Enqueue(eventItem);
            }
        }


        public TransactionsEvent GetTransactionsEvent()
        {
            lock (_transactionsEventQueue)
            {
                if (_transactionsEventQueue.Count > 0)
                    return _transactionsEventQueue.Dequeue();
                else
                    return null;
            }
        }
    }
}
